#include "SkillPool.h"
#include "Sheet.h"
#include "Skill.h"
#include <string>
using std::string;


SkillPool* SkillPool::instance()
{
	static SkillPool pool;
	return &pool;
}

Skill* SkillPool::GetSkill( int id )
{
	SMIter itFind = m_pool.find(id);
	if(itFind != m_pool.end())
	{
		return itFind->second;
	}
	else if(m_pool.size())
	{
		return (m_pool.begin())->second;
	}
	else
	{
		return 0;
	}
}

SkillPool::SkillPool()
{
	AddSkill_Sheet("..\\res\\skill.txt");
}

void SkillPool::AddSkill( int id, Skill* skill )
{
	m_pool.insert(std::make_pair<int,Skill*>(id, skill));
}

void SkillPool::AddSkill_Sheet( const char* filename )
{
	Sheet* skills = LoadSheet(filename);
	if(!skills)
		return;

	int cID		= skills->GetColumn("id");
	int cName	= skills->GetColumn("name");
	int cType	= skills->GetColumn("type");
	int cTarget	= skills->GetColumn("target");
	int cValue	= skills->GetColumn("value");
	int cDur	= skills->GetColumn("duration");
	int cStep	= skills->GetColumn("step");
	int cDesc	= skills->GetColumn("desc");



	EntryIter* itCur = skills->GetFirst();
	EntryIter* itEnd = skills->GetLast();
	while(*itCur != *itEnd)
	{
		Entry* entry = itCur->GetEntry();

		int id		= entry->GetCell(cID)->toInt();
		string name	= entry->GetCell(cName)->toStr();
		int type	= entry->GetCell(cType)->toInt();
		int target	= entry->GetCell(cTarget)->toInt();
		int val		= entry->GetCell(cValue)->toInt();	
		int dur		= entry->GetCell(cDur)->toInt();
		int step	= entry->GetCell(cStep)->toInt();
		string desc	= entry->GetCell(cDesc)->toStr();

		Skill* skill = new Skill(name, type, target, val, dur, step, desc);
		AddSkill(id, skill);

		itCur->Next();
	}
	delete skills;
}